﻿@model CharacterCreation.Business.ViewModels.VMCharacter
@using System.Linq
@{
    ViewBag.Title = "Create";
}

<h2>Create Character</h2>


@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
    
        @Html.HiddenFor(m => m.Id)

        @if (!(bool)ViewBag.IsValid)
        {
            <div class="validation-block" >
                @Html.ValidationMessageFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Description)
            </div>
        }

        <dl>
            <hr />

            <dt>@Html.LabelFor(model => model.Name)</dt>
            <dd>@Html.EditorFor(model => model.Name)</dd>
            
            <hr />

            <dt>@Html.LabelFor(model => model.Description)</dt>
            <dd>@Html.EditorFor(model => model.Description)</dd>

            <hr />
            
            <dt>@Html.LabelFor(model => model.Race)</dt>
            <dd>@Html.DropDownListFor(m => m.Race, new CharacterCreation.Business.Repositories.RaceRepository().RacesSelectListItem, new { @class = "race-select" })</dd>

            <hr />
        </dl>
        
        <!-- Character Statistics -->
        <h3>Statistics</h3>
        <table>
            <tr>
                <th>
                    Name
                </th>
                <th>
                    Rolled value
                </th>
                <th>
                    Moved value
                </th>
                <th>
                    Racial bonus
                </th>
                <th>
                    Total
                </th>
                <th>
                    Information
                </th>
            </tr>

            @for (int i = 0; i < Model.Statistics.Count; ++i )
            {
                @Html.PartialFor(m => Model.Statistics[i], "CharacterStatistic_Create")
            }
        </table>
        <hr />
        
        @if (ViewBag.ShowRolledStats)
        {
            <table>
                <tr><th>left</th><th>right</th></tr>
                @for (int i = 0; i < ViewBag.LeftStats.Count; ++i)
                {
                    <tr>
                        <td>@ViewBag.LeftStats[i]</td>
                        <td>@ViewBag.RightStats[i]</td>
                    </tr>
                }
                <tr><td>@ViewBag.LeftStatTotal</td>
                    <td>@ViewBag.RightStatTotal</td></tr>
            </table>
            <hr />
        }
        

        <input type="submit" value="Save" class="button" />
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>


<script type="text/javascript">
    $(document).ready(function () {
        $(".race-select").change(function () {
            var raceId = $(this).val();

            $.ajax({
                url: '@Url.Action("GetRace")',
                type: 'POST',
                data: { raceId: raceId },
                success: function (result) {
                    $('input[racebonus="Strength"]').val(result.StrengthBonus);
                    $('input[racebonus="Dexterity"]').val(result.DexterityBonus);
                    $('input[racebonus="Fighting"]').val(result.FightingBonus);
                    $('input[racebonus="Constitution"]').val(result.ConstitutionBonus);
                    $('input[racebonus="Perception"]').val(result.PerceptionBonus);
                    $('input[racebonus="Intelligence"]').val(result.IntelligenceBonus);
                    $('input[racebonus="Will power"]').val(result.WillPowerBonus);
                    $('input[racebonus="Charisma"]').val(result.CharismaBonus);
                    $('input[racebonus="Hit points"]').val(result.HitPointsBonus);
                    $('input[racebonus="Essence"]').val(result.EssenceBonus);
                }
            });
        });


    });


    $(document).ready(function () {
        //        $('input[statMod="true"]').change(
        //            function changeStat() {
        //                alert('qweqwe');
        //            };
        //        );
    });
</script>